<?php
namespace util\transmission;

class sftp
{
    private
        //服务器连接对象
        $connect,

        //授权对象
        $ssh_auth,

        //SFTP子系统
        $ssh_sftp
    ;

    public function __construct($host = '',  $name = '', $pass  = '', $port = 22)
    {
        if(empty($host) || empty($name) || empty($pass) || empty($port))
        {
            echo 'lack of parameter.[host/name/pass/port]';
            exit();
        }

        //连接服务器
        $this->connect = ssh2_connect($host,$port);

        if(!$this->connect)
        {
            echo "connect to the server failed.[$host:$port]\r\n";
            exit();
        }
        else
        {
            echo "connect to the server successful.\r\n";
        }

        //登陆服务器
        $this->ssh_auth = ssh2_auth_password($this->connect,$name,$pass);

        if(!$this->ssh_auth)
        {
            echo "username or password error.[name:$name,pass:$pass]";
            exit();
        }
        else
        {
            echo "sign in to the server successful.\r\n";
        }

        //转接SFTP子系统
        $this->ssh_sftp = ssh2_sftp($this->connect);

        if(!$this->ssh_sftp)
        {
            echo "using the STP subsystem failed.";
            exit();
        }
        else
        {
            echo "using the STP subsystem successful.\r\n";
        }
    }

    //创建文件夹
    public function mkDir($directory , $model = 0750, $recursive = true)
    {
        return ssh2_sftp_mkdir($this->ssh_sftp , $directory , $model , $recursive);
    }

    //上传文件 文件名包括目录
    public function upload($localFileName , $hostFileName , $permissions = 664)
    {
        echo "local:$localFileName\r\n";
        echo "host:$hostFileName\r\n";
        echo "mode:$permissions\r\n";
        echo $permissions; 
        //上传文件并返回状态
        $result = ssh2_scp_send( $this->connect , $localFileName , $hostFileName/*, $permissions*/ );
        echo $result. "\n";
    }

    //fopen上传SFTP
    public function fupload($localFileName , $hostFileName){
        //打开本地文件
        $lf = fopen($localFileName,"r");
        //打开SFTP上面的文件
        $hf = fopen("ssh2.sftp://{$this->ssh_sftp}/.{$hostFileName}", 'a+');

        if(!$lf)
        {
            echo "open file failed.\r\n";
            return false;
        }
        else
        {
            echo "open local file successful\r\n";
        }


        if(!$hf)
        {
            echo "create file failed.\r\n";
            return false;
        }
        else
        {
            echo "create host file successful. \r\n";
        }

        $i = 1;

        //逐行读取本地文件 防止文件过大内存BOOM
        while(!feof($lf))
        {
            //读取一行本地文件内容
            $data = fgets($lf);
            //如果存在
            if($data)
            {
                //写入STP文件内容中 ----
                if(fwrite($hf,$data."\r\n"))
                {
                    echo "put file successful at line $i\r\n";
                }
                else
                {
                    echo "put file failed at line:$i\r\n";
                }
            }
            else
            {
                echo "local file gets no.$i failed.\r\n";
            }

            $i++;
        }
        //关闭本地文件句柄
        fclose($lf);
        //关闭SFTP文件句柄
        fclose($hf);

        return true;
    }

    //删除文件夹
    public function delDir($directory = '')
    {
        return ssh2_sftp_rmdir($this->ssh_sftp,$directory);
    }

    //删除文件 - 文件名包括目录
    public function delFile($fileName = '')
    {
        return ssh2_sftp_unlink($this->ssh_sftp , $fileName);
    }

    //重命名
    public function rename($oldName = '' , $newName = '')
    {
        return ssh2_sftp_rename($this->ssh_sftp , $oldName , $newName);
    }
}
